<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="45%"
      @close="form = {}"
    >
      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        :inline="true"
        label-width="80px"
      >
        <el-form-item>
          <el-form-item label="归属学科" prop="subject_id">
            <el-select v-model="subject_id" placeholder="请选择">
              <el-option
                v-for="item in sub"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form-item>
        <el-form-item label="目录名称" prop="directory_name">
          <el-input
            placeholder="请输入目录名称"
            v-model="form.directory_name"
          ></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number
            v-model="form.sort"
            :min="1"
            :max="1000"
            label="描述文字"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="是否显示" prop="is_show">
          <!-- v-model="form.is_show" -->
          <!-- :value="form.is_show === 1 ? true : false" -->
          <!-- v-model="str" -->

          <el-switch
            v-model="form.is_show"
            :active-value="1"
            :inactive-value="2"
            active-text="是"
            inactive-text="否"
          >
          </el-switch>
        </el-form-item>
        <el-form-item label="目录图标" prop="directory_icon">
          <el-upload action="" list-type="picture-card">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="btnOK">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  adddirectory,
  allDirectory,
  showdirectory,
  editdirectory,
  tranListToTreeData,
  getDirectoryAllNews,
} from "@/api/subject";
export default {
  name: "addDirectory",
  data() {
    return {
      dialogVisible: false,
      limit: 10,
      page: 1,
      form: {
        sort: null,
      },
      rules: {
        subject_name: [{ required: true, message: "必填", trigger: "blur" }],
        directory_name: [{ required: true, message: "必填", trigger: "blur" }],
        is_show: [
          {
            required: true,
            message: "默认显示",
            trigger: "blur",
          },
        ],
        sort: [
          {
            required: true,
            message: "不能小于1",
            trigger: "blur",
          },
        ],
      },

      sub: [],
      tableData: [],
      subject_id: null,
    };
  },
  computed: {
    title() {
      return this.form.id ? "修改" : "添加";
    },
  },
  created() {
    this.huoqu();
    this.initData();
  },
  methods: {
    async initData() {
      let { data } = await getDirectoryAllNews({
        limit: this.limit,
        page: this.page,
      });

      this.tableData = data.list;
    },
    async huixian(row) {
      let { data } = await showdirectory(row.id);
      console.log(row.id);
      if (row.id === data.id) this.form = data;
      this.dialogVisible = true;
      console.log(this.tableData);
      var res = this.tableData.filter(function (item) {
        return item.id == row.id;
      });
      console.log(res);
      this.subject_id = res[0].subject_name;
    },

    async huoqu() {
      let { data } = await allDirectory();

      this.sub = data;
    },
    async btnOK() {
      await this.$refs.form.validate();
      console.log(this.form.id);
      if (this.form.id) {
        await editdirectory(this.form);
        this.$emit("update");
      } else {
        const { data } = await adddirectory({
          ...this.form,
          subject_id: this.subject_id,
        });
        console.log(data);
        this.$emit("update");
      }

      this.$message.success("成功");
      this.dialogVisible = false;
    },
  },
};
</script>

<style></style>
